<?php

namespace App\Http\Controllers\Api\V1\Role;

use App\Http\Validate\Api\Role\RoleRuleV;
use App\Lib\Util\ErrorCode;
use App\Services\Role\RoleService;
use Illuminate\Http\Request;

class RoleController
{

    private $RoleService;

    public function __construct(RoleService $RoleService)
    {
        $this->RoleService = $RoleService;
    }

  /**
   * 获取角色列表
   * @param Request $request
   * @return mixed
   * @throws \Exception
   */
    public function getRoleList(Request $request)
    {
      $param = $request->all();
      $validator = RoleRuleV::get($param);
      if ($validator->fails()) {
        throw new \Exception($validator->errors()->first(),ErrorCode::PARAMS_ERROR);
      }
      $arrRoleList = $this->RoleService->getRoleList();
      return response()->success($arrRoleList);
    }

  /**
   * 增加角色
   * @param Request $request
   * @return mixed
   * @throws \Exception
   */
    public function addRole(Request $request)
    {
      $param = $request->all();
      $validator = RoleRuleV::add($param);
      if ($validator->fails()) {
        throw new \Exception($validator->errors()->first(),ErrorCode::PARAMS_ERROR);
      }
      $arrMenuList = $this->RoleService->addRole($param);
      return response()->success($arrMenuList);
    }

    /**
     * 修改角色信息
     * @param Request $request
     * @return mixed
     * @throws \Exception
     */
    public function editRole(Request $request)
    {
      $param = $request->all();
      $validator = RoleRuleV::edit($param);
      if ($validator->fails()) {
        throw new \Exception($validator->errors()->first(),ErrorCode::PARAMS_ERROR);
      }
      $arrMenuList = $this->RoleService->editRole($param);
      return response()->success($arrMenuList);
    }
    /**
     * 删除角色
     * @param Request $request
     * @return mixed
     * @throws \Exception
     */
    public function delRole(Request $request)
    {
      $param = $request->all();
      $validator = RoleRuleV::del($param);
      if ($validator->fails()) {
        throw new \Exception($validator->errors()->first(),ErrorCode::PARAMS_ERROR);
      }
      $arrMenuList = $this->RoleService->delRole($param['id']);
      return response()->success($arrMenuList);
    }



}
